//
//  YJMCallerView.swift
//  Weibo
//
//  Created by 杨军民 on 16/7/18.
//  Copyright © 2016年 杨军民. All rights reserved.
//

import UIKit

//定义代理协议
protocol CallerViewDelegate: NSObjectProtocol{
    //定义代理方法
    ///注册
    func registeredEvent()
    ///登录
    func LogInEvent()
}

//访客视图
class YJMCallerView: UIView {
    
    //3、定义代理属性
    weak var delegate : CallerViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        backgroundColor = UIColor .brownColor()
//        self.backgroundColor = UIColor.color
        backgroundColor = UIColor (white: 237 / 255.0, alpha: 1)
        self .SetUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    //公开方法
    //MRAK: - 未登录控件登录方法
    func setUpCallerInfo (imageName : String , title : String ) {
        //隐藏房子
        homeView.hidden = true
        //把转轮放到后面去
        self.sendSubviewToBack(shadeView)
        //放到前面
//        self.bringSubviewToFront(iconView)
        //设置图片
        iconView.image = UIImage (named: imageName)
        //设置消息内容
        MessageLabel.text = title
    }
    
    //公开方法
    //MRAK: - 转轮转动
    func startAnimation() {
        //使用核心动画
        let start = CABasicAnimation(keyPath: "transform.rotation")
        //设置转动的范围
        start.toValue = 2 * M_PI
        //设置时间
        start.duration = 30
        //是否重复
        start.repeatCount = MAXFLOAT
        //当跳转到其他界面的时候系统会自动关闭核心动画，所以需要手动关闭
        start.removedOnCompletion = false
        //讲核心动画添加的图层中
        iconView.layer.addAnimation(start, forKey: nil)
        
    }
    
    //设置UI界面(约束)
    private func SetUpUI() {
        
        //添加子控件
        self .addSubview(iconView)
        self .addSubview(shadeView)
        self .addSubview(homeView)
        self .addSubview(MessageLabel)
        self .addSubview(registerButton)
        self .addSubview(LoginButton)
        
        //纯代码约束就要才关闭Autoresizing,避免受到影响
        iconView.translatesAutoresizingMaskIntoConstraints = false
        shadeView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        MessageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        LoginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //转轮
        /*
        参数:
        1.item: 要约束的view
        2.attribute: 要约束对象的那个属性
        3.relatedBy: 参照关系
        4.toItem: 要参照的view
        5.attribute: 要参照view的那个属性
        */
        
        //讲约束添加到View中
        //转轮
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint (item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        /*----------------------------------------------------------*/
        
        //遮罩
        self.addConstraint(NSLayoutConstraint (item: shadeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint (item: shadeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /*----------------------------------------------------------*/
        
        //家
        self.addConstraint( NSLayoutConstraint (item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint (item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /*----------------------------------------------------------*/
        
        //消息
        self.addConstraint(NSLayoutConstraint (item: MessageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //头部
        self.addConstraint(NSLayoutConstraint (item: MessageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        self.addConstraint( NSLayoutConstraint (item: MessageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
        
       /*----------------------------------------------------------*/
        
        //注册按钮
        //左边对齐
        self.addConstraint(NSLayoutConstraint (item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: MessageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //头部约束
        self.addConstraint(NSLayoutConstraint (item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: MessageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        self.addConstraint( NSLayoutConstraint (item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
        self.addConstraint( NSLayoutConstraint (item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /*----------------------------------------------------------*/
        
        //登录按钮
        //左边对齐
        self.addConstraint(NSLayoutConstraint (item: LoginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: MessageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //头部约束
        self.addConstraint(NSLayoutConstraint (item: LoginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: MessageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        self.addConstraint( NSLayoutConstraint (item: LoginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
        self.addConstraint( NSLayoutConstraint (item: LoginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
   
        
    }
    
    //MARK: - 实现注册点击事件
    @objc private func registeredEventClick() {
        print("注册")
        //调用注册代理方法
        delegate?.registeredEvent()
    }
    
    //MARK: - 实现登录点击事件
    @objc private func LogInEventClick() {
        print("登录")
        //调用登录代理方法
        delegate?.LogInEvent()
    }

    
    //MAEK - 懒加载
    //转轮
    private lazy var iconView : UIImageView = UIImageView (image: UIImage (named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩
    private lazy var shadeView : UIImageView = UIImageView (image: UIImage (named: "visitordiscover_feed_mask_smallicon"))
    
    //家
    private lazy var homeView : UIImageView = UIImageView (image: UIImage (named: "visitordiscover_feed_image_house"))
    //消息
    private lazy var MessageLabel : UILabel = {
        let label : UILabel = UILabel (frame: CGRectZero)
        
        //设置字体居中
        label.textAlignment = NSTextAlignment.Center
        //设置字体大小
        label.font = UIFont.systemFontOfSize(15)
        //设置字体颜色
        label.textColor = UIColor .orangeColor()
        //分行
        label.numberOfLines = 0
        label.text = "关注一些人,看看有什么惊喜!"
        //让控件size根据内容自己调整
        label.sizeToFit()
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton : UIButton = {
        let button = UIButton (type: UIButtonType.Custom)
        
        //设置标题
        button.setTitle("注册", forState: UIControlState.Normal)
        //设置字体颜色
        button.setTitleColor(UIColor .orangeColor(), forState: UIControlState.Normal)
        //字体的大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //背景图片
        button.setBackgroundImage(UIImage (named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: Selector("registeredEventClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //登录按钮
    private lazy var LoginButton : UIButton = {
       let button  = UIButton (type: UIButtonType.Custom)
        
        //设置标题
        button.setTitle("登录", forState: UIControlState.Normal)
        //设置字体颜色
        button.setTitleColor(UIColor .darkGrayColor(), forState: UIControlState.Normal)
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //设置背景图片
        button.setBackgroundImage(UIImage (named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: Selector("LogInEventClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
